package com.rgs.platform.controller;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageInfo;
import com.google.common.base.Strings;
import com.rgs.base.core.model.WebApiReturnResultModel;
import com.rgs.base.core.page.IPageRequestModel;
import com.rgs.base.core.page.IPageResponseModel;
import com.rgs.base.core.system.WebResponseState;
import com.rgs.base.database.DataPageUtil;
import com.rgs.model.platform.vo.LendInfoVO;
import com.rgs.platform.dto.lend.ListLendPageRequestDTO;
import com.rgs.platform.dto.lend.SubmitLendRequestDTO;
import com.rgs.platform.entity.LendInfoDO;
import com.rgs.platform.filter.LendInfoFilterDTO;
import com.rgs.platform.service.LendInfoService;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Ren Gaoshuai
 * @ClassName LendInfoController
 * @date 2022-01-24 11:27
 **/
@RestController
@RequestMapping("/v1/lend")
public class LendInfoController {

    @Resource
    LendInfoService lendInfoService;

    @ApiOperation("/获取借出详情")
    @GetMapping("/{id}")
    public WebApiReturnResultModel<LendInfoVO> selectByLendId(@PathVariable("id") String id) {
        LendInfoVO lendInfoVO = BeanUtil.copyProperties(lendInfoService.selectById(id), LendInfoVO.class);
        return WebApiReturnResultModel.ofSuccess(lendInfoVO);
    }

    @ApiOperation("新增借出")
    @PostMapping
    public WebApiReturnResultModel<Void> insert(@RequestBody @Validated SubmitLendRequestDTO submitLendRequest) {
        LendInfoDO lendInfoDO = BeanUtil.copyProperties(submitLendRequest, LendInfoDO.class);
        lendInfoService.insert(lendInfoDO);
        return WebApiReturnResultModel.ofSuccess();
    }

    @ApiOperation(value = "编辑")
    @PatchMapping
    public WebApiReturnResultModel<Void> update(@RequestBody SubmitLendRequestDTO submitLendRequest) {
        //Id不能为空
        if (Strings.isNullOrEmpty(submitLendRequest.getId())) {
            return WebApiReturnResultModel.ofStatus(WebResponseState.PARAMETER_MUST_NOT_NULL);
        }
        LendInfoDO lendInfoDO = BeanUtil.copyProperties(submitLendRequest, LendInfoDO.class);
        lendInfoService.updateSelectiveByPrimaryKey(lendInfoDO);
        return WebApiReturnResultModel.ofSuccess();
    }

    @ApiOperation("删除借出")
    @DeleteMapping("/{id}")
    public WebApiReturnResultModel<Void> delete(@PathVariable("id") String id) {
        lendInfoService.deleteById(id);
        return WebApiReturnResultModel.ofSuccess();
    }

    @ApiOperation("分页获取借出")
    @PostMapping("/page")
    public WebApiReturnResultModel<IPageResponseModel<LendInfoVO>> page(
            @RequestBody @Validated ListLendPageRequestDTO listLendPageRequest) {
        IPageResponseModel<LendInfoVO> iPageResponseModel = commonLendPage(listLendPageRequest, listLendPageRequest);
        return WebApiReturnResultModel.ofSuccess(iPageResponseModel);
    }

    private IPageResponseModel<LendInfoVO> commonLendPage(IPageRequestModel iPageRequestModel,
                                                          ListLendPageRequestDTO listLendPageRequest) {
        //获取数据
        PageInfo<LendInfoDO> pageInfo = obtainPageList(iPageRequestModel, listLendPageRequest);

        //DO转VO
        List<LendInfoVO> voList = pageInfo.getList().stream().map(q ->
                BeanUtil.copyProperties(q, LendInfoVO.class)).collect(Collectors.toList());

        return DataPageUtil.buildResponseModel(pageInfo, voList);
    }

    private PageInfo<LendInfoDO> obtainPageList(IPageRequestModel iPageRequestModel,
                                                ListLendPageRequestDTO listLendPageRequest) {
        return lendInfoService.listPageByFilter(
                iPageRequestModel,
                LendInfoFilterDTO.builder()
                        .lendType(listLendPageRequest.getLendType())
                        .lendName(listLendPageRequest.getLendName())
                        .isBack(listLendPageRequest.getIsBack())
                        .build()
        );
    }

    @ApiOperation("借出导出")
    @PostMapping("/export")
    public WebApiReturnResultModel<Void> export(
            @RequestBody @Validated ListLendPageRequestDTO listLendPageRequest) {
        return WebApiReturnResultModel.ofSuccess();
    }
}
